/*
 * HtmlComparisonDataTable.java
 *
 * Created on 2007.06.20, 22:59:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hr.ctg.component.ui;

import hr.ctg.domain.model.Mjesto;

import java.io.IOException;
import java.util.List;

import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 * 
 * @author Administrator
 */
public class HtmlOrderedDataGrid extends UIComponentBase {

	// String column;

	public HtmlOrderedDataGrid() {
	}

	@Override
	public String getFamily() {
		return null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public void encodeBegin(final FacesContext facesCtx) throws IOException {
		final int columns = Integer.parseInt((String) getAttributes().get(
				"columns"));
		final int elements = Integer.parseInt((String) getAttributes().get(
				"elements"));
		final int rows = (int) Math.ceil((double) elements / columns);
		final List<Mjesto> objekts = (List<Mjesto>) getAttributes()
				.get("value");

		final ResponseWriter rw = facesCtx.getResponseWriter();
		rw.startElement("table", this);
		for (int i = 0; i <= rows; i++) {
			rw.startElement("tr", this);
			for (int j = 0; j <= columns; j++) {
				final int index = j * rows + i;
				if (index < objekts.size()) {
					rw.startElement("td", this);
					rw.writeText(objekts.get(index).getNaziv(), null);
					rw.endElement("td");
				}
			}
			rw.endElement("tr");
		}
		rw.endElement("table");
		rw.close();

	}
}
